package com.itcast.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itcast.mapper.SkuMapper;
import com.itcast.pojo.GoodInfo;
import com.itcast.pojo.Sku;
import com.itcast.service.SkuService;
import com.itcast.util.EscUtil;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author JinX
 * @Date 1/14/2021 10:59 AM
 * @Version 1.0
 */

@Service
@Slf4j
public class SkuServiceImpl extends ServiceImpl<SkuMapper, Sku> implements SkuService {


    @Autowired
    private RestHighLevelClient client;

    private SearchSourceBuilder builder = new SearchSourceBuilder();


    @Override
    public List<Sku> findByPage(int start, int size) {
        return baseMapper.findByPage(start, size);
    }

    /**
     * 分页查找 结果封装为 GoodInfo
     *
     * @param start
     * @param size
     * @return
     */
    @Override
    public List<GoodInfo> findGoodInfoByPage(int start, int size) {
        return baseMapper.search(start,size);
    }

    /**
     * 通过id在es中找找
     *
     * @param skuId
     * @return
     */
    @Override
    public Sku findById(String skuId) throws IOException {
        GetRequest getRequest = new GetRequest("sku", skuId);
        GetResponse response = client.get(getRequest, RequestOptions.DEFAULT);
        String sourceAsString = response.getSourceAsString();
        return JSON.parseObject(sourceAsString, Sku.class);
    }

    /**
     * 根据名字查询结果
     *
     * @param name
     * @return
     */
    @Override
    public List<Sku> findByName(String name) throws IOException {

        SearchSourceBuilder builder = new SearchSourceBuilder();
        builder.query(QueryBuilders.matchQuery("name", name));
        SearchRequest searchRequest = new SearchRequest("sku");
        searchRequest.source(builder);
        SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
        SearchHits hits = response.getHits();

        ArrayList<Sku> list = new ArrayList<>();
        for (SearchHit hit : hits) {
            String string = hit.getSourceAsString();
            Sku sku = JSON.parseObject(string, Sku.class);
            list.add(sku);
        }

        return list;
    }

    /**
     * 首页关键字分页查询结果
     *
     * @param curPage
     * @param size
     * @param keyword
     * @return
     */
    @Override
    public List<Sku> search(int curPage, int size, String keyword) {

        int from = (curPage - 1) * size;
        builder.query(QueryBuilders.matchQuery("title", keyword)).from(from).size(size);

        List<Sku> list = EscUtil.highlightQuery(builder, GoodInfo.class, "title", "goods");
        return list;
    }
}
